package com.renren.project.db.validator.password;

import com.renren.project.db.validator.ValidationUtils;
import org.apache.commons.lang3.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * 密码格式是否符合
 */
public class PasswordImpl implements ConstraintValidator<Password,String> {

    private boolean required;

    @Override
    public void initialize(Password constraintAnnotation) {
        this.required = constraintAnnotation.required();
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if(required){
            return ValidationUtils.isPassword(value);
        }else{
            if(StringUtils.isBlank(value)){
                return true;
            }else{
                return ValidationUtils.isPassword(value);
            }
        }
    }
}
